package com.mc.servlet;

import com.mc.bean.Match;
import com.mc.service.MatchService;
import com.mc.service.impl.MatchServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author zl
 * 更新赛事控制器
 */
@WebServlet("/updatematch.action")
public class UpdateMatchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑对象
        MatchService matchService = new MatchServiceImpl();
        //获取用户输入的id
        String id = req.getParameter("mid");
        System.out.println(id);
        String type = req.getParameter("type");
        System.out.println(type);
        int mid = Integer.parseInt(id);
        Match match = matchService.findByMid(mid);
        if(type.equals("1")){
            req.setAttribute("match",match);
            req.getRequestDispatcher("updatematch.jsp").forward(req,resp);
        }else if(type.equals("2")){
            String mname = req.getParameter("name");
            String overview = req.getParameter("about");
            String starttime = req.getParameter("starttime");
            starttime=starttime.replace("T"," ");
            String endtime = req.getParameter("endtime");
            endtime=endtime.replace("T"," ");
            String msid = req.getParameter("msid");
            String mtid = req.getParameter("mtid");
            int  sid = Integer.parseInt(msid);
            int tid = Integer.parseInt(mtid);

            try {
                Timestamp startTime = new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(starttime).getTime());
                Timestamp endTime = new Timestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(endtime).getTime());
                match.setMname(mname);
                match.setOverview(overview);
                match.setStarttime(startTime);
                match.setEndtime(endTime);
                match.setMsid(sid);
                match.setMtid(tid);
                boolean flag = matchService.update(match);
                System.out.println(flag);
                System.out.println(flag);
                System.out.println(mname); 
                System.out.println(overview);
                System.out.println(starttime);
                System.out.println(endtime);
                System.out.println(msid);
                System.out.println(mtid);

                resp.sendRedirect("matchlist.action");

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
